package com.fetch.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JProgressBar;
import javax.swing.Timer;

import com.download.exception.DownloadException;
import com.download.utils.AudioUtil;
import com.download.utils.DownloadHelper;

public class FinishActionListener implements ActionListener {

    private JProgressBar progressBar;
    private JButton openFile;
    private JButton openFolder;
    
    public FinishActionListener(JProgressBar progressBar, JButton openFile,
            JButton openFolder) {
        super();
        this.progressBar = progressBar;
        this.openFile = openFile;
        this.openFolder = openFolder;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            float currentProcess = DownloadHelper.getDownloadProcess();
            progressBar.setValue((int)(currentProcess * 100));
            if(currentProcess == 1.0){
                //进度条到100%
                progressBar.setValue((int)currentProcess * 100);
                openFile.setEnabled(true);
                openFolder.setEnabled(true);
                //播放提示音
                AudioUtil.play("src/main/resources/download-complete.wav");
                if(e.getSource() instanceof Timer ){
                    ((Timer)e.getSource()).stop();
                }
            }
        } catch (IOException e1) {
            throw new DownloadException("DownloadActionListener.actionPerformed()", e1);
        }
    }

}
